﻿using System;
using System.Linq;
using System.Text;

namespace PicShare.Server.Helpers
{
    public static class LoginRegisterHelper
    {
        private const int MinLength = 4;
        private const int MaxLength = 20;
   

        private const string ValidUsernameCharacters =
            "qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM1234567890_.";
        private const string ValidNicknameCharacters =
            "qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM1234567890_. -";

        private const string SessionKeyChars =
           "qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM";

        private static readonly Random Rand = new Random();

        private const int SessionKeyLength = 50;

        private const int Sha1Length = 40;


        internal static void ValidateUserName(string userName)
        {

            if (userName == null)
            {
                throw new ArgumentNullException
                    ("UserName can not be null");
            }
            else if (userName.Length < MinLength)
            {
                throw new ArgumentOutOfRangeException
                    (string.Format("Username at least be {0} characters long", MinLength));
            }
            else if (userName.Length > MaxLength)
            {
                throw new ArgumentOutOfRangeException
                    (string.Format("Username can not be more thant {0} characters long", MaxLength));
            }
            else if (userName.Any(ch => !ValidUsernameCharacters.Contains(ch)))
            {
                throw new ArgumentOutOfRangeException
                    (string.Format("Username must contain only Latin letters , digits and .,_"));
            }

        }
        #region Validate NickName
        //        internal static void ValidateNickName(string nickName)
//        {
//            if (nickName == null)
//            {
//                throw new ArgumentNullException
//                    ("Nickname can not be null");
//            }
//            else if (nickName.Length < MinLength)
//            {
//                throw new ArgumentOutOfRangeException
//                    (string.Format("Nickname at least be {0} characters long", MinLength));
//            }
//            else if (nickName.Length > MaxLength)
//            {
//                throw new ArgumentOutOfRangeException
//                    (string.Format("Nickname can not be more thant {0} characters long", MaxLength));
//            }
//            else if (nickName.Any(ch => !ValidUsernameCharacters.Contains(ch)))
//            {
//                throw new ArgumentOutOfRangeException
//                    (string.Format("Nickname must contain only Latin letters , digits and .,_"));
//            }
        //        }
        #endregion
        internal static void ValidateAuthCode(string authCode)
        {
            if (authCode == null || authCode.Length < Sha1Length)
            {
                throw new ArgumentOutOfRangeException("Password shold be encripted");
            }
        }

        internal static string GenerateSessionKey(string userId)
        {
            var skeyBuilder = new StringBuilder(SessionKeyLength);
            skeyBuilder.Append(userId);
            while (skeyBuilder.Length < SessionKeyLength)
            {
                var index = Rand.Next(SessionKeyChars.Length);
                skeyBuilder.Append(SessionKeyChars[index]);
            }
            return skeyBuilder.ToString();
        }

        internal static void ValidateSessionKey(string sessionKey)
        {
            if (sessionKey.Length != SessionKeyLength || sessionKey.Any(ch => !SessionKeyChars.Contains(ch)))
            {
                throw new ArgumentException("Invalid Password", "ERR_INV_AUTH");
            }
        }
    }
}